package com.andaily.domain.dto.log;

import com.andaily.domain.dto.AbstractDTO;
import com.andaily.domain.dto.user.UserDto;
import com.andaily.domain.log.Log;
import com.andaily.domain.log.LogRepository;
import com.andaily.domain.log.LogVisibility;
import com.andaily.domain.shared.DateUtils;
import com.andaily.domain.shared.security.SecurityUtils;
import com.andaily.web.context.BeanProvider;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Shengzhao Li
 */
public class LogDto extends AbstractDTO {

    protected static LogRepository logRepository() {
        return BeanProvider.getBean(LogRepository.class);
    }

    protected String content;
    protected String longitude;
    protected String latitude;
    protected int commentSize;

    protected String logDate;
    protected LogTypeDto logType;
    protected String visibility;

    protected UserDto user;
    protected boolean addNext;
    protected String createTime;
    protected String createFullTime;

    protected Boolean allowRecordPosition;

    public LogDto() {
        this.logDate = DateUtils.toDateText(DateUtils.now());
        this.visibility = LogVisibility.PUBLIC.name();
    }

    public LogDto(Log log) {
        this.guid = log.guid();
        this.content = log.content();
        this.logDate = DateUtils.toDateText(log.logDate());

        this.visibility = log.visibility().name();
        this.logType = new LogTypeDto(log.type());

        final Date cTime = log.createTime();
        this.createTime = DateUtils.toDateText(cTime, DateUtils.TIME_FORMAT);
        this.createFullTime = DateUtils.toDateTime(cTime);

        this.user = new UserDto(log.owner());
        this.commentSize = log.logComments().size();

        BigDecimal latitude1 = log.latitude();
        if (latitude1 != null) {
            this.latitude = latitude1.toString();
        }
        BigDecimal longitude1 = log.longitude();
        if (longitude1 != null) {
            this.longitude = longitude1.toString();
        }
    }

    public boolean isPrivateLog() {
        return LogVisibility.PRIVATE.name().equals(this.visibility);
    }


    public Boolean getAllowRecordPosition() {
        return allowRecordPosition;
    }

    public void setAllowRecordPosition(Boolean allowRecordPosition) {
        this.allowRecordPosition = allowRecordPosition;
    }

    public String getCreateFullTime() {
        return createFullTime;
    }

    public int getCommentSize() {
        return commentSize;
    }

    public void setCommentSize(int commentSize) {
        this.commentSize = commentSize;
    }

    public boolean isLogged() {
        return SecurityUtils.isLogged();
    }

    public boolean isAllowEdit() {
        return (this.user != null && this.user.getGuid().equals(SecurityUtils.currentUserGuid()));
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public boolean isAddNext() {
        return addNext;
    }

    public void setAddNext(boolean addNext) {
        this.addNext = addNext;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLogDate() {
        return logDate;
    }

    public void setLogDate(String logDate) {
        this.logDate = logDate;
    }

    public LogTypeDto getLogType() {
        return logType;
    }

    public void setLogType(LogTypeDto logType) {
        this.logType = logType;
    }

    public String getVisibility() {
        return visibility;
    }

    public void setVisibility(String visibility) {
        this.visibility = visibility;
    }

    public UserDto getUser() {
        return user;
    }

    public void setUser(UserDto user) {
        this.user = user;
    }

    public Log instance() {
        return (isNewly() ? new Log() : logRepository().findByGuid(this.guid));
    }

    public static List<LogDto> toDtos(List<Log> logs) {
        List<LogDto> list = new ArrayList<LogDto>(logs.size());
        for (Log log : logs) {
            list.add(new LogDto(log));
        }
        return list;
    }

    public String getCreateTime() {
        return createTime;
    }
}
